﻿using System;
using System.Web.Security;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Paradox.Models;
using Paradox.Models.DataModels;
using Paradox.Models.ViewModels;
namespace Paradox.Controllers
{





    public class StoryController : Controller
    {

        //
        // GET: /Story/22
        StoryRepository repo = new StoryRepository();
        
        public ActionResult Index(int id)
        {
            var story = repo.getFullStory(id).ToList().FirstOrDefault();
            var comments = repo.GetCommentsForStory(id).ToList();
            var tags = repo.getTags(id).ToList();
            ViewData["StoryText"] = story;
            ViewData["StoryComments"] = comments;
            ViewData["StoryTags"] = tags;

            return View();
        }




        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Index(Comment comment)  //Post A New Comment
        //{

        //    if (!ModelState.IsValid)
        //    {
        //        return PartialView("PostComment");
        //    }
        //    else
        //    {
        //        var dbComment = new Comment();
        //        dbComment.comment_posted_by = comment.comment_posted_by;
        //        dbComment.comment_body = comment.comment_body;
        //        dbComment.story_id = comment.story_id;
        //        dbComment.poster_ip = dbComment.poster_ip;

        //        return PartialView("CommentPosted");
        //    }
        //}

        [Authorize(Roles = "elevateduser")]
        public ActionResult CreateStory()
        {
            return View("NewStory");
        }

        [Authorize(Roles = "elevateduser")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateStory(Story story)
        {
            if (!ModelState.IsValid)
            {
                 return RedirectToAction("Story/CreateStory");
            }else{

                repo.PostNewStory(story);
                repo.Save();
               
                 


                return View("StoryPosted");
                }
        }


        public ActionResult CreateTag()
        {
            return View("CreateTag");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public void CreateTag(Tag tag)
        {
            using (RSSDataContext db = new RSSDataContext())
            {

               

                repo.NewTag(tag);
                repo.Save();

                RedirectToAction("Story/CreateTag");
                   
                
                   
                
                 
            }
                

                
        }

        
    
    }
}

